<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once ("session_controller.php");

class Profile extends Session_controller {
	
	function __construct(){
		parent::__construct();
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="help-block">', '</div>');
		$this->load->helper('form');
	}
	
	function index(){
		$this->data['titulo'] = "Perfil";
		$this->add_css(base_url(CSS_PATH . 'profile.css'));
		$this->show('profile_view');
	}
	
	function edit(){
		$this->data['titulo'] = "Editar Perfil";
		$this->form_validation->set_rules('nombre', 'Nombre', 'trim|required|xss_clean');
		$this->form_validation->set_rules('apellido','Apellido', 'trim|required|xss_clean');
		$this->form_validation->set_rules('descripcion', 'Descripción', 'trim|xss_clean');
		if($this->form_validation->run()){
			$jugador['nombre'] = $this->input->post('nombre');
			$jugador['apellido'] = $this->input->post('apellido');
			$jugador['descripcion'] = $this->input->post('descripcion');
			$this->jugador->update($jugador, $this->data['jugador']->id);
			redirect('home/logout');
		}else{
			$this->show('profile_edit');
		}
	}
	
	function image_edit(){
		$this->data['error'] = '';
		$this->show('profile_image_edit');
	}
	
	public function upload_image(){
		/* Configuración del upload */
		$config['upload_path'] = UPDIMG_PATH;
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1024';
		$config['max_width']  = '1024';
		$config['width'] = '128';
		$config['max_height']  = '768';
		/* Encrypting helps prevent the file name from being discerned once its saved */
		$config['encrypt_name'] = 'TRUE';
		
		/* Load the CodeIgniter upload library, feed it the config from above */
		$this->load->library('upload', $config);

		if(!$this->upload->do_upload()){
			$this->data['error'] = $this->upload->display_errors();
			$this->show('profile_image_edit');
		}else{
			$image_file = $this->upload->data()['file_name'];
			$this->jugador->update(array('imagen' => $image_file), $this->data['jugador']->id);
			redirect('profile', 'refresh');
		}	
	}
}